//
//  HRTabBarController.swift
//  XMLA
//
//  Created by Gao on 17/2/16.
//  Copyright © 2017年 Odm. All rights reserved.
//

import UIKit

class HRTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // TabBarController绑定, 配置各个控制器
        
        // findNavi是套有导航的
        let findNavi = HRFindViewController()
        setupChildController(findNavi, imageName: "tabbar_find_n", selectedImage: "tabbar_find_h")
        
        let soundVC = HRSoundViewController()
        self.setupChildController(soundVC, imageName: "tabbar_sound_n", selectedImage: "tabbar_sound_h")
        
        // 只占用空间
        let vc = UIViewController()
        self.setupChildController(vc, imageName: nil, selectedImage: nil)
        
        let downloadVC = HRDownloadViewController()
        self.setupChildController(downloadVC, imageName: "tabbar_download_n", selectedImage: "tabbar_download_h")
        
        // 从独立的storyboard中获取一个控制器
//        var meVC: HRMeViewController? = kStoryboard("MeSetting").instantiateViewController(withIdentifier: "Me")
//        self.setupChildController(meVC, imageName: "tabbar_me_n", selectedImage: "tabbar_me_h")
        let meVC = HRMeViewController()
        self.setupChildController(meVC, imageName: "tabbar_me_n", selectedImage: "tabbar_download_h")
        
        // 设置tabbar的背景图
        self.tabBar.backgroundImage = UIImage(named: "tabbar_bg")
    }

    /**  绑定各个控制器, 顺便设置属性 */    
    func setupChildController(_ vc: UIViewController, imageName imgName: String?, selectedImage selectedImgName: String?) {
        // 设置图片间距
        vc.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0)
        if let imgName = imgName {
            vc.tabBarItem.image = UIImage(named: imgName)
        }
        
        // 设置图片的不渲染
        if let selectedImgName = selectedImgName {
            let image = UIImage(named: selectedImgName)?.withRenderingMode(.alwaysOriginal)
            vc.tabBarItem.selectedImage = image
        }
        
        
        addChildViewController(vc)
    }

}
